<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

class LettermanModelSubscribers extends JModel
{

	var $_data = null;

	var $_total = null;
        
        var $_pagination = null;

	function __construct()
	{
		parent::__construct();

		global $mainframe, $option;                

		// Get the pagination request variables
		$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart	= $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );

		// In case limit has been changed, adjust limitstart accordingly
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}

	function getData()
	{
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_data;
	}
        
        function _buildWhere ()
        {
            global $mainframe, $option;

            $search = $mainframe->getUserStateFromRequest( $option.'search','search','','string' );
            $search = JString::strtolower( $search );

            $where = array();
            
            $where[] = "s.campaign = c.id" ;
            if ($search) {
                    $where[] = 'LOWER(s.subscriber_email) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
            }           
           
            $where = ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );

            return $where;           

        }
        
	function _buildOrder()
	{
		global $mainframe, $option;

		$filter_order = JRequest::getCmd('order','s.subscribe_date');
		$filter_order_Dir = $mainframe->getUserStateFromRequest( $option.'filter_order_Dir', 'filter_order_Dir','', 'word' );

		if ($filter_order == 's.subscribe_date'){
			$order	= ' ORDER BY s.subscribe_date '.$filter_order_Dir;
		} else {
			$order 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir.' , s.subscribe_date ';
		}
                
		return $orderby;
	}         

	function getTotal()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	function _buildQuery()
	{
                $query = "SELECT s.*, "
                        ."c.title AS campaign_name "
                        ."FROM #__letterman_subscribers AS s, "
                        ."#__letterman_campaigns AS c "
                        . $this->_buildWhere()
                        . $this->_buildOrder();

		return $query;
	}
        
	function getPagination()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}        
}
?>